﻿using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using System.Data.SqlClient;
using DAL;
using SqlSugar.Extensions;

namespace BLL
{
    public class B_Group
    {
        DAL.D_Group d_Group = new DAL.D_Group();
        DAL.D_G d_G = new DAL.D_G();

        public List<Stu_group> getList()
        {
            return d_Group.getlist();
        }

        public bool save_List(List<Stu_group> groups)
        {
            return d_Group.save_List(groups);
        }

        public Stu_group GetGroup(Student student)
        {
            return d_Group.GetGroup(student);
        }

        public bool Update_Group(Models.Stu_group group)
        {
            var res = d_Group.update(group);
            return res;
        }

        public bool release(Models.Stu_group stu_Group) 
        {
            bool res = false;
            try
            {
                using (TransactionScope tx = new TransactionScope())//事务处理
                {
                    d_Group.release(stu_Group);//删除组
                    d_G.Del(stu_Group);//删除g_data中对应的数据

                    BLL.B_Student student = new BLL.B_Student();
                    int rows = student.release_groupid(stu_Group);//更新每个学生有关学生组的信息

                    if (rows != stu_Group.members)
                        tx.Dispose();
                    else
                    {
                        tx.Complete();
                        log_info.student.is_gleader = -1;
                        log_info.student.group_id = -1;
                        res = true;
                    }
                }
            }
            catch (SqlException ex)
            {
                int error_code = ex.ErrorCode;
                throw new Tools.Del_Exception(string.Format("删除失败！ sql 错误代码{0}", error_code));
            }
            catch (Exception e)
            {   throw e;  }
            
            return res;
        }

        public bool Commit(Models.Stu_group stu_Group, Models.Student student)
        {
            DAL.D_Student d_student = new DAL.D_Student();
            try
            {
                //检查传入的学生对象student是否是组长（is_gleader属性值为1）。
                if (student.is_gleader == 1)
                {
                    //更新传入的学生分组对象stu_Group。
                    d_Group.update(stu_Group);
                    return true;
                }

                if (student.group_id == -1)
                {
                    //创建新的组，更新相关的数据，并将当前学生设置为组长
                    using (TransactionScope tx = new TransactionScope())//事务处理
                    {
                        stu_Group.members = 1;
                        stu_Group.group_lead = student.stu_id;
                        int group_id = d_Group.Add(stu_Group);

                        stu_Group.group_id = group_id;
                        student.group_id = group_id;
                        student.is_gleader = 1;

                        d_student.Update_groupid_suger(student);

                        tx.Complete();
                       
                    }

                    return true;
                }
                else
                { return false; }
            }
            catch
            {
                throw new Tools.Modify_Exception("修改失败！");
            }
        }

        public bool Add_student(List<Models.Student> group_student, Models.Stu_group group)
        {
            BLL.B_Student b_Student = new BLL.B_Student();
            BLL.B_Group b_Group = new BLL.B_Group();
            string msg;

            bool res;
            try
            {
                using (TransactionScope tx = new TransactionScope())//事务处理
                {
                    res = b_Student.Update_groupid(group_student, group.group_id, out msg);
                    if (!res)
                        tx.Dispose();
                    else
                    {
                        group.members += group_student.Count;
                        b_Group.Update_Group(group);
                        tx.Complete();
                        res = true;
                    }
                }
            }
            catch
            {
                throw new Tools.Modify_Exception("修改失败！");
            }

            return res;
        }

        public bool Remove_student(List<Models.Student> group_student, Models.Stu_group group)
        {
            BLL.B_Student b_Student = new BLL.B_Student();
            BLL.B_Group b_Group = new BLL.B_Group();
            string msg;

            bool res;
            try
            {
                using (TransactionScope tx = new TransactionScope())//事务处理
                {
                    res = b_Student.Update_groupid(group_student, -1, out msg);
                    if (!res) tx.Dispose();

                    group.members -= group_student.Count;
                    b_Group.Update_Group(group);
                    tx.Complete();
                    res = true;
                }
            }
            catch
            {
                throw new Tools.Modify_Exception("删除组成员失败！");
            }

            return res;
        }

    }

}
